package com.situ.personal.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.situ.personal.entity.PageInfo;
import com.situ.personal.entity.User;
import com.situ.personal.service.UserService;

@WebServlet("/user")
public class UserServlet extends HttpServlet {
	
	UserService userService = new UserService(); 
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		// 处理Post请求的乱码
		//req.setCharacterEncoding("UTF-8");
		
		// 判断当前是否 是 登录状态
//		HttpSession httpSession = req.getSession(); // 获取那块Session
//		User user = (User) httpSession.getAttribute("user");
//		if (user==null) {
//			System.out.println("UserServlet当前是否登陆, user:"+user);
//			resp.sendRedirect(req.getContextPath()+"/user_login.jsp");
//			return;
//		}
		
		
		// 接收 方法
		String method  = req.getParameter("method");
		
		//如果 忘记传入 挥着 第一次进入   默认值 findByPage
		if ( method == null || "".equals(method) ) {
			method = "findByPage";
		}
		System.out.println("UserServlet现在执行的方法"+ method);
		switch (method) {
		case "findByPage":
			findByPage(req,resp);
			break;
		case "insert":
			insert(req,resp);
			break;
		case "deleteById":
			deleteById(req,resp);
			break;
		case "selectById":
			selectById(req,resp);
			break;
		case "updateById":
			updateById(req,resp);
			break;

		default:
			break;
		}
		
	}// service 

	// 真正的修改
	private void updateById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		req.setCharacterEncoding("UTF-8");
		int id = Integer.parseInt( req.getParameter("id") );
		String name = req.getParameter("name");
		int age = Integer.parseInt( req.getParameter("age") );
		String password = req.getParameter("password");
		String email = req.getParameter("email");
		int departmentId = Integer.parseInt( req.getParameter("departmentId") );
		User user = new User(id, name, age, password, email, departmentId);  // 现在里面存的  就是  改好user信息
		System.out.println("UserServlet/updateById"+user);
		
		// 把这个user对象传给service  让他 在sql里user.getXxx();改
		boolean isSuccess = userService.updateById(user);
		if (isSuccess) {
			System.out.println("修改成功");
		}else {
			System.out.println("修改失败");
		}
		
		resp.sendRedirect(req.getContextPath()+"/user");
	}

	// 先查出来   要改的  user 原来的信息
	private void selectById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String id = req.getParameter("id");
		User user = userService.selectById(Integer.parseInt(id));
		System.out.println("Servlet层57行(selectById)"+user);
		req.setAttribute("user", user);
		req.getRequestDispatcher("/department?method=updateById").forward(req, resp);
	}

	// 删除功能
	private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		
		// 删user -- 先删光 userId对应的所有RoleId(一个user有多个角色role对应多个role_id) -- 再删userId    (一对多，要删一，先把多全删光，才能删一)
		// User 关联 UserRoleId  ,有外键，，1个userId 对应 多个RoleId,所以UserId要删除，必须 先 把对应的多个roleId删除，
		// 在service层(以上属于 业务逻辑)，先执行userDao.deleteRoleByUserId,,,把这个userId对相应的多个roleId全删了(user_role表)
		// roleId全被删了，，，，，关联的userId就没有对应的东西了
		// 在执行 deleteById(userId)   就把 user信息也删了 
		
		String id = req.getParameter("id");
		boolean isSuccess = userService.deleteById(Integer.parseInt(id));
		if (isSuccess) {
			System.out.println("删除成功");
		}else {
			System.out.println("删除失败");
		}
		resp.sendRedirect(req.getContextPath()+"/user"); // 进user 默认找findByPage方法，所以 ?method写不写都好
		// resp.sendRedirect(req.getContextPath()+"/user?method=findByPage");
	}

	//添加功能
	private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String name = req.getParameter("name");
		String age = req.getParameter("age");
		String password = req.getParameter("password");
		String email = req.getParameter("email");
		String departmentId = req.getParameter("departmentId");
		User user = new User(name, Integer.parseInt(age), password, email, Integer.parseInt(departmentId));
		System.out.println("添加信息："+user);
		
		boolean isSuccess = userService.insert(user);
		if (isSuccess) {
			System.out.println("添加功能：插入成功");
		}else {
			System.out.println("添加功能：插入失败");
		}
		resp.sendRedirect(req.getContextPath()+"/user?method=findByPage");
		
	}

	private void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String pageNo = req.getParameter("pageNo");
		String pageSize = req.getParameter("pageSize");
		if (pageNo == null || "".equals(pageNo)) {
			pageNo = "1";
		}
		if (pageSize == null || "".equals(pageSize)) {
			pageSize = "5";
		}
		PageInfo pageInfo = userService.findByPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
		System.out.println(pageInfo);
		
		req.setAttribute("pageInfo", pageInfo);
		req.getRequestDispatcher("/user_page_list.jsp").forward(req, resp);	
//		ArrayList<User> list = (ArrayList<User>) pageInfo.getList();
//		req.setAttribute("list", list);
//		req.getRequestDispatcher("/JSTL_.jsp").forward(req, resp);
	}//findByPage
}
